//
//  ShoppingCartViewCellCollectionViewCell.m
//  YYBG_IOS
//
//  Created by wyl on 2017/3/8.
//  Copyright © 2017年 wyl. All rights reserved.
//

#import "ShoppingCartViewCell.h"
#import "UIView+Extension.h"
#import "UIImageView+Extension.h"
#define CELL_HEIGHT 160
@interface ShoppingCartViewCell() <UITextFieldDelegate>

/**期数*/
@property (nonatomic,strong) UILabel *titleLabel;
/**商品*/
@property (nonatomic,strong) UILabel *detailLabel;
/**数量*/
@property (nonatomic,strong) UITextField *numbersTf;
/**商品图片*/
@property (nonatomic,strong) UIImageView *goodsImage;

//@property (nonatomic,strong) ShopCartModel *cartModel ;

@property (nonatomic,strong) UIButton *chooseImage;//选择框

/**分享值*/
@property (nonatomic,strong) UILabel *shareValue;
/**最新价格*/
@property (nonatomic,strong) UILabel *newestValue;
///**旧价格*/
@property (nonatomic,strong) UILabel *oldValue;

@end

static NSString *ID = @"ShoppingCartViewCell";
@implementation ShoppingCartViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView{
    ShoppingCartViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[ShoppingCartViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}

- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

/**
 *  初始化控件
 */
- (void) setupUI{
    //加 按钮
    _addBtn = [[UIButton alloc]init];
    [_addBtn setImage:ImageNamed(@"add") forState:UIControlStateNormal];
    _addBtn.tag = 1;
    [_addBtn addTarget:self action:@selector(goodsCountNumClick:) forControlEvents:UIControlEventTouchUpInside];
    [_addBtn setBorder:1 cornerRadius:0];
    [self.contentView addSubview:_addBtn];
    
    //减 按钮
    _reduceBtn = [[UIButton alloc]init];
    [_reduceBtn setImage:ImageNamed(@"reduce") forState:UIControlStateNormal];
    _reduceBtn.tag = 2;
     [_reduceBtn setBorder:1 cornerRadius:0];
    [_reduceBtn addTarget:self action:@selector(goodsCountNumClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_reduceBtn];
    
    //图片名字
    _goodsImage = [[UIImageView alloc]init];
    [_goodsImage setImage:ImageNamed(nil)];
    [self.contentView addSubview:_goodsImage];
    
    //标题
    _titleLabel = [[UILabel alloc]init];
    _titleLabel.font = [UIFont systemFontOfSize:15];
    _titleLabel.numberOfLines = 0;
    _titleLabel.textColor = RGBA(51, 51, 51, 1);
    [self.contentView addSubview:_titleLabel];
 
    // 产品
    _detailLabel = [[UILabel alloc]init];
    _detailLabel.font = [UIFont systemFontOfSize:13];
    _detailLabel.numberOfLines = 1;
    _detailLabel.textColor = RGBA(153, 153, 153, 1);
    _detailLabel.numberOfLines= 2 ;
    [self.contentView addSubview:_detailLabel];
 
    
    //数量
    _numbersTf = [[UITextField alloc]init];
    _numbersTf.textAlignment = NSTextAlignmentCenter;
    _numbersTf.keyboardType = UIKeyboardTypeNumberPad;
    _numbersTf.delegate = self ;
    [_numbersTf setBorder:1 cornerRadius:0];
    _numbersTf.font =[UIFont systemFontOfSize:13];
    _numbersTf .background = ImageNamed(@"shopping_cell_textfield_bj_53x39_");
    [_numbersTf addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.contentView addSubview:_numbersTf];
    
    
    
    //删除选诓
    _chooseImage = [[UIButton alloc]init];
    [_chooseImage addTarget:self action:@selector(chooseCell:) forControlEvents:UIControlEventTouchUpInside];
    [_chooseImage setImage:ImageNamed(@"checkbox1") forState:UIControlStateNormal];
    [_chooseImage setImage:ImageNamed(@"checkbox2") forState:UIControlStateSelected];
    [_chooseImage setImage:ImageNamed(@"checkbox2") forState:UIControlStateHighlighted];
    [self.contentView addSubview:_chooseImage];
    
    //分享值
    _shareValue = [[UILabel alloc]init];
    _shareValue.font = [UIFont systemFontOfSize:13];
    _shareValue.numberOfLines = 0;
    _shareValue.textColor =  RGBA(51, 51, 51, 1);
    [self.contentView addSubview:_shareValue];
    
    //新价格
    _newestValue = [[UILabel alloc]init];
    _newestValue.font = [UIFont systemFontOfSize:15];
    _newestValue.numberOfLines = 0;
    _newestValue.textColor = [UIColor orangeColor];
    [self.contentView addSubview:_newestValue];
    
    _oldValue = [[UILabel alloc]init];
    _oldValue.font = [UIFont systemFontOfSize:13];
    _oldValue.numberOfLines = 0;
    _oldValue.textColor =  RGBA(153, 153, 153, 1);
    [self.contentView addSubview:_oldValue];
    
    [self setUIFrame];
}

/**
 *  设置frame
 */
- (void) setUIFrame{
    CGFloat btnW = 25;
    
    _chooseImage.frame = CGRectMake(0, 10, 40 , 100);
    _chooseImage.centerY = CELL_HEIGHT / 2 ;
    
    _goodsImage.frame = CGRectMake(_chooseImage.maxX , 10, 80,80);
    _goodsImage.centerY = CELL_HEIGHT / 2 ;
    
    _titleLabel.frame = CGRectMake(CGRectGetMaxX(_goodsImage.frame)+12, 10, 300, 25);
    
    _detailLabel.frame = CGRectMake(CGRectGetMaxX(_goodsImage.frame)+12,CGRectGetMaxY(_titleLabel.frame)+2, APPWIDTH - _goodsImage.maxY  - 46, 46);
    
    //分享值
    _shareValue.frame = CGRectMake(CGRectGetMaxX(_goodsImage.frame)+12,CGRectGetMaxY(_detailLabel.frame)+2,120, 20);
    //新价格
    _newestValue.frame = CGRectMake(CGRectGetMaxX(_goodsImage.frame)+12,CGRectGetMaxY(_shareValue.frame)+10,50, 20);
    //旧价格
    _oldValue.frame = CGRectMake(CGRectGetMaxX(_newestValue.frame)+3,_newestValue.y,70, 20);
    
    _newestValue.text = @"¥100";
    [_newestValue sizeToFit];
    _oldValue.x = _newestValue.maxX + 2 ;
    _oldValue.text =@"价格:¥200";
    [_oldValue sizeToFit];
    
    _reduceBtn.frame = CGRectMake(CGRectGetMaxX(_oldValue.frame)+20, CGRectGetMaxY(_shareValue.frame)+2, btnW, btnW);
    _numbersTf.frame = CGRectMake(CGRectGetMaxX(_reduceBtn.frame)-1.5,  CGRectGetMaxY(_shareValue.frame)+2, btnW+10, btnW);
    _addBtn.frame = CGRectMake(CGRectGetMaxX(_numbersTf.frame)-1.5,  CGRectGetMaxY(_shareValue.frame)+2, btnW, btnW);
    
    _titleLabel.text = @"标题";
    _detailLabel.text = @"商品描述商品描述商品描述商品描述商品描述商品描述商品描述商品描述商品描述";
    _shareValue.text = @"分享值:60";
  
    _numbersTf.text = @"50";
   
}
/**
 *  模型赋值
 */
//- (void)setModel:(ShopCartModel *)model
//{
//    self.cartModel = model ;
//    [YYBGCommon setImageWithUrl:self.goodsImage url:[YYBGCommon getImageUrl:model.good_img]];
//    if ( ChangeToFloat(model.yunjiage) == 10) {
//        [self.goodsImage addLabelToTop:YES];
//    }else {
//        [self.goodsImage addLabelToTop:NO];
//    }
//    [_deleteImage setImage:ImageNamed(@"shoppingcart_delete_nomal_20x21_") forState:UIControlStateNormal];
//    _deleteImage.hidden = YES ;
//    self.numbersTf.text =[NSString stringWithFormat:@"%d",model.cart_num];//数量
//    if(self.numbersTf.text.intValue>1){
//        self.reduceBtn.enabled = YES;
//    }
//    
//    NSString *shengyuStr = [NSString stringWithFormat:@"剩余%d人次",model.shenyurenshu] ;
//    self.surplusLb.attributedText = [YYBGCommon getArrtributeString:shengyuStr attword: [NSString stringWithFormat:@"%d",model.shenyurenshu] color:[UIColor orangeColor]];
//    
//    
//    if (model.isNewGoods) {
//        self.periodLb.text =[NSString stringWithFormat:@" 已更新至:(第%@期) ",model.qishu];
//        
//        self.periodLb.numberOfLines = 1 ;
//        [self.periodLb sizeToFit];
//        
//        
//        self.periodLb.layer.cornerRadius = 4 ;
//        self.periodLb.layer.borderWidth=1.0f;
//        self.periodLb.layer.borderColor= [UIColor orangeColor].CGColor;
//        _periodLb.textColor = [UIColor orangeColor];
//    }else {
//        self.periodLb.text =[NSString stringWithFormat:@"(第%@期) ",model.qishu];
//        
//        self.periodLb.numberOfLines = 1 ;
//        [self.periodLb sizeToFit];
//        
//        
//        self.periodLb.layer.cornerRadius = 0 ;
//        self.periodLb.layer.borderWidth= 0;
//        self.periodLb.textColor =[UIColor blackColor];
//    }
//    
//    
//    self.goodsLb.x = self.periodLb.maxX ;
//    self.goodsLb.text = model.title;//商品标题
//    
//    [_goodsLb sizeToFit];
//    _goodsLb.width = APPWIDTH- 10 - _goodsImage.width - 10 - self.periodLb.width - 20 ;
//    self.goodsLb.x = CGRectGetMaxX( self.periodLb.frame) ;
//    _timeLb.text = @" 人次";
//    if (model.isChoose) {
//        [_chooseImage setSelected:YES];
//    }else {
//        [_chooseImage setSelected:NO];
//    }
//}




/**
 *  编辑数量的点击方法
 */
- (void)goodsCountNumClick:(UIButton *)sender
{
    
    if([self.delegate respondsToSelector:@selector(shoppingCellDelegateForGoodsCount:WithCountButton:)])
    {
        [self.delegate shoppingCellDelegateForGoodsCount:self WithCountButton:sender];
    }
}



-(void)deleteCell{
//    [self.delegate deleteCell:self.cartModel];
}
-(void)chooseCell:(UIButton *)sender{
//    if (sender.isSelected) {
//        [sender setSelected:NO];
//        [self.delegate unChooseCell:self.cartModel];
//    }else {
//        [sender setSelected:YES];
//        [self.delegate chooseCell:self.cartModel];
//    }
    
}
-(void)textFieldDidChange :(UITextField *)theTextField{
//    NSInteger number = ChangeToInteger(theTextField.text);
//    if (number >= self.cartModel.shenyurenshu) {
//        self.cartModel.cart_num = self.cartModel.shenyurenshu;
//    }
//    else {
//        self.cartModel.cart_num = number ;
//    }
//    theTextField.text = [NSString stringWithFormat:@"%ld",self.cartModel.cart_num];
//    [self.delegate numberChageCell:self];
}

// 失去焦点
- (void)textFieldDidEndEditing:(UITextField *)textField{
//    NSLog(@"失去焦点");
//    NSInteger number = ChangeToInteger(textField.text);
//    if (number == 0) {
//        textField.text = [NSString stringWithFormat:@"%d",0];
//        self.cartModel.cart_num = 1;
//        if (self.delegate) {
//            [self.delegate updateAllTableView];
//        }
//        
//    }
}
@end
